import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:gd_assistant/page/public/colors.dart';
import 'package:gd_assistant/page/public/publicView.dart';
import 'package:gd_assistant/page/public/themeUtils.dart';
import 'package:gd_assistant/util/dataUtil.dart';
import 'package:gd_assistant/api/apiHandler.dart';

class changePassWordPage extends StatefulWidget {
  @override
  _changePassWordPageState createState() => _changePassWordPageState();
}

class _changePassWordPageState extends State<changePassWordPage> {
  bool isDark = false;
  bool isButtonEnable=true;  //按钮状态 是否可点击
  String buttonText='发送验证码'; //初始文本
  int count=60;      //初始倒计时时间
  Timer timer;      //倒计时的计时器
  TextEditingController passWordController=TextEditingController();
  TextEditingController codeController=TextEditingController();
    
  void _buttonClickListen(){
  setState(() {
    if(isButtonEnable){   //当按钮可点击时
    isButtonEnable=false; //按钮状态标记
    _initTimer();
      return null;   //返回null按钮禁止点击
    }else{     //当按钮不可点击时
      return null;    //返回null按钮禁止点击
    }
  });
  }
    
    
  void _initTimer(){
  timer = new Timer.periodic(Duration(seconds: 1), (Timer timer) {
    count--;
    setState(() {
    if(count==0){
    timer.cancel();    //倒计时结束取消定时器
    isButtonEnable=true;  //按钮可点击
    count=60;     //重置时间
    buttonText='发送验证码';  //重置按钮文本
    }else{
    buttonText='重新发送($count)'; //更新文本内容
    }
    });
  });
  }
    
    
  @override
  void dispose() {
    timer?.cancel();  //销毁计时器
    timer=null;
    super.dispose();
    passWordController.dispose();
    codeController.dispose();
  }
    
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    passWordController.addListener(() { setState(() {});});
    codeController.addListener(() { setState(() {}); });
  }
  @override
  Widget build(BuildContext context) {
    isDark = ThemeUtils.isDark(context);
    Color bg_color = isDark ? Colours.dark_bg_gray : Colors.white;
    Color text_color = isDark ? Colours.dark_text : Color(0xff333333);
    return Scaffold(
      appBar: AppBar(
        title: Text("修改密码"),
      ),
      body: Container(
        child: ListView(
        children: <Widget>[
          Padding(padding: EdgeInsets.only(top: 20)),
          Container(
              height: 50,
              color: bg_color,
              padding: EdgeInsets.only(left: 20,right: 20),
              child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              textBaseline: TextBaseline.ideographic,
              children: <Widget>[
              Text('手机号',style: TextStyle(fontSize: 18,color: text_color)),
              Expanded(
                child: Padding(padding: EdgeInsets.only(left: 15,right: 15),
                child: Text(dataUtil.getValue(dataUtil.assistantPhoneKey), style: TextStyle(fontSize: 16))
                ),
              )
            ],
            ),
          ),
          Container(
              color: bg_color,
              padding: EdgeInsets.only(left: 20,right: 20),
              child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              textBaseline: TextBaseline.ideographic,
              children: <Widget>[
              Text('新密码',style: TextStyle(fontSize: 18,color: text_color),),
              Expanded(
                child: Padding(padding: EdgeInsets.only(left: 15,right: 15,top: 15),
                child: TextFormField(
                maxLines: 1,
                onSaved: (value) { },
                controller: passWordController,
                textAlign: TextAlign.left,
                decoration: InputDecoration(
                hintText: ('填写新密码'),
                contentPadding: EdgeInsets.only(top: -30,bottom: 0),
                hintStyle: TextStyle(
                color: Color(0xff999999),
                fontSize: 13,
                ),
                alignLabelWithHint: true,
                border: OutlineInputBorder(borderSide: BorderSide.none),
                ),
                ),),
              )
            ],
            ),
          ),
            Container(
              color: bg_color,
              padding: EdgeInsets.only(left: 20,right: 20),
              child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              textBaseline: TextBaseline.ideographic,
              children: <Widget>[
              Text('验证码',style: TextStyle(fontSize: 18,color: text_color),),
              Expanded(
                child: Padding(padding: EdgeInsets.only(left: 15,right: 15,top: 15),
                child: TextFormField(
                maxLines: 1,
                onSaved: (value) { },
                controller: codeController,
                textAlign: TextAlign.left,
                inputFormatters: [WhitelistingTextInputFormatter.digitsOnly, LengthLimitingTextInputFormatter(6)],
                decoration: InputDecoration(
                hintText: ('填写验证码'),
                contentPadding: EdgeInsets.only(top: -30,bottom: 0),
                hintStyle: TextStyle(
                color: Color(0xff999999),
                fontSize: 13,
                ),
                alignLabelWithHint: true,
                border: OutlineInputBorder(borderSide: BorderSide.none),
                ),
                ),),
              ),
              Container(
                width: 120,
                child: FlatButton(
                disabledColor: Colors.grey.withOpacity(0.1),  //按钮禁用时的颜色
                disabledTextColor: Colors.white,     //按钮禁用时的文本颜色
                textColor:isButtonEnable?Colors.white:Colors.black.withOpacity(0.2),       //文本颜色
                color: isButtonEnable?Colors.blue:Colors.grey.withOpacity(0.1),       //按钮的颜色
                splashColor: isButtonEnable?Colors.white.withOpacity(0.1):Colors.transparent,
                shape: StadiumBorder(side: BorderSide.none),
                onPressed: () async { 
                  await apiHandler.getAuthcode(dataUtil.getValue(dataUtil.assistantPhoneKey));
                  setState(() {
                    _buttonClickListen();
                  }
                );},
                child: Text('$buttonText',style: TextStyle(fontSize: 13)),
                ),
              ),
            ],
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 20),
            child: ZLButton(
              title: "确认",
              onTap: passWordController.text.length > 0 &&  codeController.text.length > 0 ? () async {
                  TostUtil.showLoading(context, "修改密码中...");
                  dynamic re = await apiHandler.changePassWord(passWordController.text, codeController.text);
                  TostUtil.removeLoading(context);
                  if (re != null) {
                    Navigator.of(context).pop();
                  }
              } : null,
            ),
          )
          ],
        ),
      ),
    );
  }
}